package com.qf.Controller;

import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class OrderController {
//    @Autowired
//    EurekaClient eurekaClient;
//    //自动注入RestTemplate对象
//    @Autowired
//    RestTemplate template;
    @Autowired
    TestService testService;
    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        testService.clearCache(1);
        System.out.println(testService.test(1));
    }
    @Autowired     
    GoodsFeign goodsFeign;
    @RequestMapping("/query/{id}")
    @HystrixCommand(fallbackMethod="fallback")
    public String query(@PathVariable Integer id){
        //根据Eureka上注册的服务名称获取实例对象
//        InstanceInfo instanceInfo= eurekaClient.getNextServerFromEureka("qf-goods",false);
//        //获取对应模块的url
//        String url=instanceInfo.getHomePageUrl();
//        ResponseEntity<String> responseEntity
//                =template.getForEntity("http://QF-GOODS/query",String.class);
//        String body = responseEntity.getBody();

        //返回结果
        //String query = goodsFeign.query();
        //String byId = goodsFeign.findGoods(new Goods(23,"小杰",24.3,"宇宙无敌大帅哥"));
        String query = goodsFeign.query(id);
        return "查询订单"+query;

    }
    public String fallback(Integer id){
        System.out.println("服务降级了");
        return "服务器降级了";
    }
}
